/***************************************************************************//**
 * @file MSc_XmlNode.h
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2014 Marek M. Cel
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/
#ifndef MSC_XML_NODE_H
#define MSC_XML_NODE_H

////////////////////////////////////////////////////////////////////////////////

#include <map>
#include <string>

#include <libxml/tree.h>

////////////////////////////////////////////////////////////////////////////////

/** */
class MSc_XmlNode
{
public:

    typedef std::map<std::string,std::string> Attributes;

    /** Constructor. */
    MSc_XmlNode( xmlNodePtr node = 0 );

    /** Destrcutor. */
    virtual ~MSc_XmlNode() {}

    /**
     * Returns the value of the attribute or an empty string if the attribute
     * has not been specified.
     */
    std::string getAttribute( std::string name );

    /**
     * Returns element attributes list or empty list if the node is not
     * an element or element does not has any attributes.
     */
    Attributes getAttributes();

    /**
     * Returns first child node.
     */
    MSc_XmlNode getFirstChild();

    /**
     * Returns first child element node of the given name.
     * @param name element name
     */
    MSc_XmlNode getFirstChildElement( std::string name );

    /**
     * Returns node name.
     */
    std::string getName() const
    {
        return std::string( (const char*)m_node->name );
    }

    /**
     * Returns next sibling node.
     */
    MSc_XmlNode getNextSibling();

    /**
     * Returns next sibling element node of the given name.
     * @param name element name
     */
    MSc_XmlNode getNextSiblingElement( std::string name );

    /** */
    std::string getText() const;

    /**
     * Returns true if node has attributes.
     */
    inline bool hasAttributes() const
    {
        if ( isValid() )
        {
            return ( m_node->properties != NULL );
        }

        return false;
    }

    /**
     * Returns true if node has children.
     */
    inline bool hasChildren() const
    {
        if ( isValid() )
        {
            return ( m_node->children != NULL );
        }

        return false;
    }

    /**
     * Returns true if node is attribute.
     */
    inline bool isAttribute() const
    {
        if ( isValid() )
        {
            return ( m_node->type == XML_ATTRIBUTE_NODE );
        }

        return false;
    }

    /**
     * Returns true if node is comment.
     */
    inline bool isComment() const
    {
        if ( isValid() )
        {
            return ( m_node->type == XML_COMMENT_NODE );
        }

        return false;
    }

    /**
     * Returns true if node is element.
     */
    inline bool isElement() const
    {
        if ( isValid() )
        {
            return ( m_node->type == XML_ELEMENT_NODE );
        }

        return false;
    }

    /**
     * Returns true if node is text.
     */
    inline bool isText() const
    {
        if ( isValid() )
        {
            return ( m_node->type == XML_TEXT_NODE );
        }

        return false;
    }

    /**
     * Returns true if node is valid.
     */
    inline bool isValid() const
    {
        return ( m_node ) ? true : false;
    }

protected:

    xmlNodePtr m_node;  ///< XML node pointer
};

////////////////////////////////////////////////////////////////////////////////

#endif // MSC_XML_NODE_H
